package com.swdegao.api.callback;

import com.alibaba.fastjson2.JSONObject;
import com.swdegao.api.util.RocketMQUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.xml.bind.DatatypeConverter;
import java.io.IOException;
@Slf4j
@RestController
@RequestMapping("/Api/GuoXinNBCallBack")
public class GuoXinNBCallBackApi {
    @Autowired
    private RocketMQUtils rocketMQUtils;

    private static String topic = "guoxinnbTopic";
    private static String tag = "guoxinnbtag01";
    @PostMapping("/callback")
    public  void callback(@RequestBody String reslut) throws IOException {
        log.info(reslut);
        JSONObject jo = JSONObject.parseObject(reslut);
        String deviceId = "";
        String imei = "";
        try {
            deviceId = jo.getString("deviceId");
            imei = jo.getString("IMEI");
        }
        catch(Exception e)
        {
        }
        byte[] data = null;
        String data_ = "";
        if (jo.get("payload") != null)
        {
            data_ = jo.getJSONObject("payload").getString("APPdata");
           // byte[] decbuff = hexStringToByte(data_);
            //data = decbuff;
            data =  DatatypeConverter.parseBase64Binary(data_);
            jo.getJSONObject("payload").put("APPdata",DatatypeConverter.printHexBinary(data));
            log.info(DatatypeConverter.printHexBinary(data));
        }

        if (data == null) return;
        switch (data[8])
        {
            case 0x04://主动上报，每条应用回复应答，并存储
                try {
                    //回复
                    //deviceCommandsDataGX(requestData);
                }
                catch(Exception e)
                {
                    log.error("deviceCommandsDataGX异常"+e.getMessage());
                }
                try
                {
                    rocketMQUtils.sendMessage(imei,topic,tag, jo.toJSONString());
                }
                catch (Exception e)
                {
                    log.error("callBackSubscribeForAEPGX异常" + e.getMessage());
                }
                break;
            case 0x62:
                //deviceCommands(deviceId, accessToken, AppID);
                //命令请求
                break;
            default:
                break;
        }

    }
    private byte[] hexStringToByte(String hex)
    {
        int len = (hex.length() / 2); //除以2是因为十六进制比如a1使用两个字符代表一个byte
        byte[] result = new byte[len];
        char[] achar = hex.toCharArray();
        for (int i = 0; i < len; i++)
        {
            //乘以2是因为十六进制比如a1使用两个字符代表一个byte,pos代表的是数组的位置
            //第一个16进制数的起始位置是0第二个是2以此类推
            int pos = i * 2;
            //<<4位就是乘以16  比如说十六进制的"11",在这里也就是1*16|1,而其中的"|"或运算就相当于十进制中的加法运算
            //如00010000|00000001结果就是00010001 而00010000就有点类似于十进制中10而00000001相当于十进制中的1，与是其中的或运算就相当于是10+1(此处说法可能不太对，)
            result[i] = (byte)(toByte(achar[pos]) << 4 | toByte(achar[pos + 1]));
        }
        return result;
    }
    private byte toByte(char c)
    {
        byte b = (byte)"0123456789ABCDEF".indexOf(c);
        return b;
    }
}
